package com.sx.mvvmlibs.base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * @Author: YY
 * @Date: 2022/1/8
 * @Describe: TODO
 */
abstract class DataBindingBaseFragment<V : ViewDataBinding, VM : BaseViewModel<out BaseModel>>(
    @LayoutRes  private val layoutId: Int,
    private val varViewModelId: Int? = null,
    sharedViewModel: Boolean = false
) : ViewBindingBaseFragment<V, VM>(sharedViewModel) {

    final override fun initBinding(inflater: LayoutInflater, container: ViewGroup?): V =
        DataBindingUtil.inflate(inflater, layoutId, container, false)

    override fun initViewModel() {
        super.initViewModel()
        if(varViewModelId != null) {
            mBinding.setVariable(varViewModelId, mViewModel)
        }
        mBinding.lifecycleOwner = viewLifecycleOwner
    }

    override fun onDestroyView() {
        mBinding.unbind()
        super.onDestroyView()
    }

}